﻿$(document).ready(main);

function main() {
//    insertar();
//    buscar();
//    modificar();
    $(".test1-button-insert").bind('click', function () {
        test1();
        return false;
    });
    $("#insert").bind('click', function () {
        insertar();
        return false;
    });
    $("#category-input-searchfield").bind('keyup', function () {
        buscar();
    });
    $(".category-button-search").bind('click', function () {
        buscar();
        return false;
    });
    $(".category-button-edit").bind('click', function () {
        modificar();
        return false;
    });

}

function test1() {

    window.location.href = "../../Flujo/Index"
    //var jsonData = JSON.stringify({ msg: "hola" });
    //$.ajax({
    //    type: "post",
    //    data: jsonData,
    //    dataType: "json",
    //    url: "/GestionCategoria/test1",
    //    contentType: "application/json; charset=utf-8",
    //    success: function (data) {
    //        alert(data);
    //    }
    //});
   // $.ajax({
     //   type: "get",
       // data: jsonData,
       // dataType: "jsonp",
      //  url: "http://credisoft.servehttp.com:8080/jw/web/json/workflow/package/list",
      //  contentType: "application/json; charset=utf-8",
      //  success: function (data) {
      //      alert(data.data[0].packageName);
      //      alert(data.data[1].packageName); 
         
      //  }
   // });

    //$.get("http://credisoft.servehttp.com:8080/jw/web/json/workflow/package/list",function(data,status){
    //    alert("Data: " + data + "\nStatus: " + status);
    //});
}

function insertar() {
    var enviar = {};
    enviar.CategoryName = $("#category-input-name").val();
    enviar.Description = $("#category-input-description").val();
    if (enviar.CategoryName === "") {
        alert("Debe ingresar el nombre de la categoría");
        return false;
    }
    if (enviar.Description === "") {
        alert("Debe ingresar la descripción de la categoría");
        return false;
    }
    console.log(enviar);

    var jsonData = JSON.stringify(enviar);

    $.ajax({
        type: "POST",
        data: jsonData,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "/GestionCategoria/insert",
        success: function (data) {
            $(".category-response").html("");
            $(".category-response").append($("<span>").text("Se insertó la categoría " + enviar.CategoryName).delay(800).fadeOut(400));
            console.log(data);
        }
    });
}

function buscar() {
    var enviar = {};
    enviar.CategoryName = $("#category-input-searchfield").val();
    console.log(enviar);

    var jsonData = JSON.stringify(enviar);

    $.ajax({
        type: "POST",
        data: jsonData,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "/GestionCategoria/search",
        success: function (data) {
            $(".category-search-results").html("");
            $.each(data, function () {
                var current_id = this.CategoryID;
                var category_div = $("<div>").attr("class", "category-search-result").attr("value", this.CategoryID);
                $(category_div).append($("<h4>").text("Categoria: "+this.CategoryName));
                $(category_div).append($("<p>").text("" + this.Description));
                $(category_div).append($("<a>").attr("class", "btn blue category-button-edit").attr("data-toggle","modal").attr("href","#responsive").text("Modificar").bind("click", function () {
                    var title = $("<h4>").text("Modificar Categoría");
                    var cat_name = $(this).parent().children("h4").text().split(" ");
                    var input1 = $("<p>").append($("<input>").attr("class", "span12").attr("type", "text").attr("id", "category-modal-name").attr("value", cat_name[1]));
                    var cat_desc = $(this).parent().children("p").text();
                    var input2 = $("<p>").append($("<input>").attr("class","span12").attr("type","text").attr("id","category-modal-description").attr("value",cat_desc));
                    $("#modal-ajax-content").html("");
                    $("#modal-ajax-content").append(title, input1, input2);
                    var boton_close = $("<button>").attr("type","button").attr("data-dismiss","modal").attr("class","btn").text("Cerrar");
                    var boton_save = $("<button>").attr("type", "button").attr("class", "btn btn-primary").attr("data-dismiss", "modal").text("Modificar").bind("click", function () {
                        modificar(current_id);
                        buscar();
                    });
                    $("#modal-footer-content").html("");
                    $("#modal-footer-content").append(boton_close,boton_save);
      
                }));
                $(category_div).append($("<button>").attr("class", "btn red category-button-delete").text("Eliminar").bind("click", function () {
                    eliminar(current_id);
                    buscar();
                    return false;
                }));

                $(".category-search-results").append(category_div);
            });
            console.log(data);
        }
    });
}

function modificar(id) {
    var enviar = {
        CategoryID: id,
        CategoryName: $("#category-modal-name").val(),
        Description: $("#category-modal-description").val(),
    };

    console.log(enviar);

    var jsonData = JSON.stringify(enviar);

    $.ajax({
        type: "POST",
        async: false,
        data: jsonData,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "/GestionCategoria/modify",
        success: function (data) {
            $(".category-response").html("");
            $(".category-response").append($("<span>").text(data).delay(800).fadeOut(400));

            console.log(data);
        }
    });
}

function eliminar(id) {
    var enviar = {
        CategoryID: id,
        CategoryName: "name",
        Description: "desc"
    };

    var jsonData = JSON.stringify(enviar);

    $.ajax({
        type: "POST",
        async: false,
        data: jsonData,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "/GestionCategoria/delete",
        success: function (data) {
            $(".category-response").html("").fadeIn(400);
            $(".category-response").append($("<span>").text(data).delay(800).fadeOut(400));


            console.log(data);
        }
    });

}